package com.lovi.io_;


import org.junit.jupiter.api.Test;

import java.io.FileReader;

/**
 * 字符流读取
 */
public class FileReader01 {
    /**
     * 单字符读取
     * 读取汉字都明显好起来了！
     * @throws Exception
     */
    @Test
    public void readFile01() throws Exception{

        String filePath = "D:\\test\\hello.txt";
        FileReader fr = new FileReader(filePath);
        int readChar = 0;
        while ((readChar = fr.read()) != -1){
            System.out.print((char)readChar);
        }
        fr.close();
    }

    /**
     * 批量字符读取
     * 读取汉字都明显好起来了！
     * @throws Exception
     */
    @Test
    public void readFile02() throws Exception{

        String filePath = "D:\\test\\hello.txt";
        FileReader fr = new FileReader(filePath);
        int readChar = 0;
        char[] chs = new char[1024];
        while ((readChar = fr.read(chs)) != -1){
            System.out.println(new String(chs,0,readChar));
        }
        fr.close();
    }
}
